<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Hash;
use Mail;
class LoginController extends Controller
{
    //注册
    public function register(Request $request)
    {

    	if($request -> isMethod('POST'))
		{
			$this -> validate($request,[
				'email' => 'required|email|unique:users',
				'password' => 'required|confirmed',
				'password_confirmation' => 'required',
				],[
				'email.required' => '邮箱不能为空',
				'email.email' => '邮箱不合法',
				'password.required' => '密码不能为空',
				'password.confirmed' => '确认密码不一致',
				'password_confirmation.required' => '确认密码不能为空',
				'email.unique' => '该邮箱已被注册',
				]);
			$data = $request -> except('_token','password_confirmation','check');
			$data['password'] = Hash::make($data['password']);
			$time = time();
			$data['add_time'] = $time;
			$data['update_time'] = $time;
			$data['name'] = 'xl_'.str_random(7);
			$data['remember_token'] = str_random(50);
			$res = DB::table('users') -> insertGetId($data);
            DB::table('userdetail') -> insert(['uid' => $res]);
            
			if($res)
			{
				// Mail::raw('这是一封注册邮件',function($message){
				// 	$message -> to('178581029@qq.com');
				// 	$message -> subject('迅雷账号激活');
				// });

				Mail::send('home.mails.active',['data' => $data],function($message) use ($data) {
					$message -> to($data['email']);
					$message -> subject('迅雷账号激活');
				});

				//获取邮箱
				$email = strstr($data['email'],'@');
				$email = ltrim($email,'@');
				return view('home.mails.toemail',['email' => $email]);
			}else
			{
				return back() -> with(['info' => '注册失败']);
			}
		}

		return view('home.login');
    }

    public function active($remember_token)
    {
    	$res = DB::table('users') -> where('remember_token',$remember_token) -> update(['is_active' => 1]);

    	if($res)
    	{
    		return redirect('/home/register') -> with(['info' => '激活成功，请登录']);
    	}else
    	{
    		return '激活失败';
    	}
    }

    //忘记密码
    public function forget(Request $request)
    {
    	if($request -> isMethod('post'))
    	{
    		$data = $request -> except('_token');
    		$res = DB::table('users') -> where('email',$data['email']) -> first();
    		if($res)
    		{
	    		Mail::send('home.mails.inputemail',['res' => $res],function($message) use ($data) {
						$message -> to($data['email']);
						$message -> subject('迅雷帐号密码找回');
				});

	    		$email = strstr($data['email'],'@');
	    		$email = ltrim($email,'@');

	    		return view('home.mails.rtoemail',['email' => $email]);

    		}else
    		{
    			return back() -> with(['info' => '请输入注册过的邮箱']);
    		}
    	}
    	return view('home.forget');
    }

    //重置密码
    public function setpass(Request $request)
    {
    	$this -> validate($request,[
    		'password' => 'confirmed',

    		],[
    		'password.confirmed' => '确认密码不一致',
    		]);
    	$data = $request -> except('_token','password_confirmation');
    	$data['password'] = Hash::make($data['password']);
    	$res = DB::table('users') -> where('email',$data['email']) -> update(['password' => $data['password']]);
    	if($res)
    	{
    		return redirect('/home/register') -> with(['info' => '修改成功']);
    	}else
    	{
    		return redirect('/home/forget') -> with(['info' => '修改失败,请重试']);
    	}
    }

    public function inputpass($remember_token)
    {
    	$email = DB::table('users') -> where('remember_token' , $remember_token) -> first() -> email;
    	return view('home.newpass',['email' => $email]);
    }
}
